package com.itheima.service;

import com.itheima.bean.User;
import com.itheima.dao.UserDaoImpl;
import com.itheima.exception.ExceptionEnum;
import com.itheima.exception.UserException;
import com.itheima.model.UserModel;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService{

    @Override
    public User login(String username, String password) throws Exception {
        // 1. 通过用户名获取该用户
        final User userFinded = new UserDaoImpl().findUserByUserName(username);
        if (userFinded == null) {
            throw new UserException(ExceptionEnum.USER_LOGIN_ERROR, "没有查询到这个用户名，还未注册.");
        }
        if (password.equals(userFinded.getPassword())) {
            System.out.println("用户 " + username + " 登录成功.");
            return userFinded;
        } else {
            throw new UserException(ExceptionEnum.USER_LOGIN_ERROR, "密码不对，请重试.");
        }
    }

    @Override
    public void register(User user) throws Exception {
        new UserDaoImpl().register(user);
    }
}
